package com.zoweunion.mechanic.service.order.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.zoweunion.mechanic.dao.order.GGOrderDao;
import com.zoweunion.mechanic.service.order.GGOrderService;
import com.zoweunion.mechanic.util.constants.Constants;

/**
 * 高管工单业务类
 * 
 * @author Administrator
 */
@Service
@Transactional
public class GGOrderServiceImpl implements GGOrderService {

	@Autowired
	private GGOrderDao gGOrderDao;

	/**
	 * 
	 * @param reqMap
	 * @return
	 */
	public Map<String, Object> getKanBanInfo(Map<String, Object> reqMap) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取所有工单
		List<Map<String, Object>> orderInfoList = gGOrderDao.getOrderInfoList(reqMap);
		// 今日工单
		List<Map<String, Object>> todayOrderInfoList = null;
		// 今日预约
		List<Map<String, Object>> todayYuYueOrderInfoList = null;
		// 今日受理
		List<Map<String, Object>> todayShouLiOrderInfoList = null;
		// 今日出工
		List<Map<String, Object>> todayChuGongOrderInfoList = null;
		// 获取今日完成工单
		List<Map<String, Object>> todayFinishOrderInfoList = null;
		// 获取历史完成工单
		List<Map<String, Object>> liShiFinishOrderInfoList = null;
		// 历史工单
		List<Map<String, Object>> liShiOrderInfoList = null;
		LocalDate today = LocalDate.now();
		if (orderInfoList != null && orderInfoList.size() > 0) {
			// 获得今日工单
			todayOrderInfoList = orderInfoList.stream().filter(map -> {
				
				if (today.toString().equals(map.get("order_creat_time").toString().substring(0, 10))) {
					return true;
				} else {
					return false;
				}

			}).collect(Collectors.toCollection(ArrayList::new));

			if (todayOrderInfoList != null && todayOrderInfoList.size() > 0) {
				// 获取今日预约工单
				todayYuYueOrderInfoList = todayOrderInfoList.stream().filter(map -> {
					// 如果工单的当前状态大于工单修工出工时的状态,则就为今日预约工单
					if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_DSL) {
						return true;
					} else {
						return false;
					}

				}).collect(Collectors.toList());
				// 获取今日受理单
				todayShouLiOrderInfoList = todayOrderInfoList.stream().filter(map -> {
					// 如果工单的当前状态大于工单修工出工时的状态,则就为今日受理工单
					if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_DTZ) {
						return true;
					} else {
						return false;
					}

				}).collect(Collectors.toList());

			}
			// 获取今日出工单
			todayChuGongOrderInfoList = todayOrderInfoList.stream().filter(map -> {
				// 如果工单的当前状态大于工单修工出工时的状态,则就为今日出工工单
				if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_YLL) {
					return true;
				} else {
					return false;
				}

			}).collect(Collectors.toList());
			// 获取今日完成工单
			todayFinishOrderInfoList = todayOrderInfoList.stream().filter(map -> {
				// 如果工单的当前状态大于工单修工出工时的状态,则就为今日完成工单
				if (Integer.valueOf(map.get("order_status").toString()) > Constants.ORDER_STATUS_DZF) {
					return true;
				} else {
					return false;
				}

			}).collect(Collectors.toList());
			
			// 获得历史工单
			liShiOrderInfoList = orderInfoList.stream().filter(map -> {
				if(reqMap.containsKey("dateTime")) {
					//截取年
					if(Integer.valueOf(reqMap.get("dateTime").toString().length())==4) {
						if (reqMap.get("dateTime").toString().equals(map.get("order_creat_time").toString().substring(0, 4))) {
							return true;
						} else {
							return false;
						}
					}
					//截取年月
					if(reqMap.get("dateTime").toString().length()==7) {
						if (reqMap.get("dateTime").toString().equals(map.get("order_creat_time").toString().substring(0, 7))) {
							return true;
						} else {
							return false;
						}
					}
					//截取年月日
					if(reqMap.get("dateTime").toString().length()==10) {
						if (reqMap.get("dateTime").toString().equals(map.get("order_creat_time").toString().substring(0, 10))) {
							return true;
						} else {
							return false;
						}
					}
				}else {
					//获取前一天的
					if (today.minusDays(1).toString().equals(map.get("order_creat_time").toString().substring(0, 10))) {
						return true;
					} else {
						return false;
					}
				}
				return false;


			}).collect(Collectors.toCollection(ArrayList::new));
			
			// 获取历史完成工单
			liShiFinishOrderInfoList = liShiOrderInfoList.stream().filter(map -> {
				// 如果工单的当前状态大于工单修工出工时的状态,则就为今日完成工单
				if (Integer.valueOf(map.get("order_status").toString()) > Constants.ORDER_STATUS_DZF) {
					return true;
				} else {
					return false;
				}

			}).collect(Collectors.toList());
		}
		//编辑历史工单
		Map<String, Object> liShiMap = weiXiOrBaoyangCount(liShiFinishOrderInfoList);
		liShiMap.remove("yuBaoYang");
		liShiMap.put("orderCount", (liShiFinishOrderInfoList != null && liShiFinishOrderInfoList.size() > 0)?liShiFinishOrderInfoList.size():0);
		resultMap.put("todayYuYue", weiXiOrBaoyangCount(todayYuYueOrderInfoList));
		resultMap.put("todayShouLi", weiXiOrBaoyangCount(todayShouLiOrderInfoList));
		resultMap.put("todayChuGong", weiXiOrBaoyangCount(todayChuGongOrderInfoList));
		resultMap.put("todayFinish", weiXiOrBaoyangCount(todayFinishOrderInfoList));
		resultMap.put("shouRuCount", shouRuCount(todayFinishOrderInfoList));
		resultMap.put("liShiCount", shouRuCount(liShiFinishOrderInfoList));
		resultMap.put("liShiFinish", liShiMap);
		
		
		
		return resultMap;
	}

	/**
	 * 获取保养维修总数 
	 * @param mapList
	 * @return
	 */
	private Map<String, Object> weiXiOrBaoyangCount(List<Map<String, Object>> mapList){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//维修
		int yuWeiXi = 0;
		//保养
		int yuBaoYang = 0;
		// 循环获取维修保养返修数据
		if(mapList != null && mapList.size() > 0 ) {
		for (Map<String, Object> yuyueMap : mapList) {
			//维修
			if (Integer.valueOf(yuyueMap.get("order_type").toString()) == Constants.ORDER_TYEP_WX) {
				yuWeiXi =yuWeiXi+1;
			}
			//保养
			if(Integer.valueOf(yuyueMap.get("order_type").toString()) == Constants.ORDER_TYEP_BY) {
				yuBaoYang =yuBaoYang+1;
			}
			//维修加保养算维修
			if(Integer.valueOf(yuyueMap.get("order_type").toString()) == Constants.ORDER_TYEP_WXBY) {
				yuWeiXi =yuWeiXi+1;
			}
			//返修
			if(Integer.valueOf(yuyueMap.get("order_type").toString()) == Constants.ORDER_TYEP_FX) {
				//获取父级工单
				Map<String, Object> orderInfo = gGOrderDao.getOrderInfo(yuyueMap.get("parent_id").toString());
				//维修
				if (Integer.valueOf(orderInfo.get("order_type").toString()) == Constants.ORDER_TYEP_WX) {
					yuWeiXi =yuWeiXi+1;
				}
				//保养
				if(Integer.valueOf(orderInfo.get("order_type").toString()) == Constants.ORDER_TYEP_BY) {
					yuBaoYang =yuBaoYang+1;
				}
				//维修加保养算维修
				if(Integer.valueOf(orderInfo.get("order_type").toString()) == Constants.ORDER_TYEP_WXBY) {
					yuWeiXi =yuWeiXi+1;
				}
			}
		}
		}
		resultMap.put("yuWeiXi", yuWeiXi);
		resultMap.put("yuBaoYang", yuBaoYang);
		return resultMap;
	}
	
	/**
	 * 获取今日收入总数 
	 * @param mapList
	 * @return
	 */
	private Map<String, Object> shouRuCount(List<Map<String, Object>> mapList){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//信用额度
		BigDecimal xinYong = new BigDecimal("0");
		//总收入
		BigDecimal zongCount = new BigDecimal("0");
		// 循环获取收入数据
		if(mapList != null && mapList.size() > 0 ) {
		for (Map<String, Object> shouRuMap : mapList) {
			//信用额度
			if (StringUtils.equals(shouRuMap.get("payment_status").toString(), "2") && StringUtils.equals(shouRuMap.get("payment_type").toString(), "1") ) {
				xinYong = xinYong.add(new BigDecimal(shouRuMap.get("payment_amount").toString()));
			}
			//总收入
			if(StringUtils.equals(shouRuMap.get("payment_status").toString(), "2")) {
				zongCount =zongCount.add(new BigDecimal(shouRuMap.get("payment_amount").toString()));
			}

		}
		}
		resultMap.put("xinYong", xinYong.setScale(2, RoundingMode.DOWN));
		resultMap.put("zongCount", zongCount.setScale(2, RoundingMode.DOWN));
		return resultMap;
	}
	
}
